package com.hongdee.scm.monthlybalance.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.DateUtils;
import com.hongdee.scm.monthlybalance.service.ITScmMonthlyBalanceService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/6/27.
 */
@RestController
@RequestMapping("/scm/monthlyBalance")
public class TScmMonthlyBalanceController {
    @Autowired
    private ITScmMonthlyBalanceService itScmMonthlyBalanceService;

    /**
     * 申请月结按钮
     * @param uid
     * @param token
     * @param month
     * @return
     * @throws Exception
     */
    @RequestMapping("/applyMonthlyBalance")
    @ResponseBody
    public ResultEntity applyMonthlyBalance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                            @RequestParam(name = "token", defaultValue = "") String token,
                                            @RequestParam(name = "month", defaultValue = "") String month)
    throws Exception{
        this.itScmMonthlyBalanceService.applyMonthlyBalance(uid,month);
        ResultEntity res = new ResultEntity();
        res.setMsg("申请月结成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 月结按钮
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/monthlyBalance")
    @ResponseBody
    public ResultEntity monthlyBalance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        this.itScmMonthlyBalanceService.monthlyBalance(uid, id);
        ResultEntity res = new ResultEntity();
        res.setMsg("月结成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 反月结按钮
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/unMonthlyBalance")
    @ResponseBody
    public ResultEntity unMonthlyBalance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        this.itScmMonthlyBalanceService.unMonthlyBalance(uid, id);
        ResultEntity res = new ResultEntity();
        res.setMsg("操作成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 月结分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/mangins")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = this.itScmMonthlyBalanceService.manginsPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 月结成本差异分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/diffCost")
    @ResponseBody
    public Map diffCost(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "data", defaultValue = "50") String data) throws Exception {

        Map map = this.itScmMonthlyBalanceService.monthCostDiff(data,uid,limit,start);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 成本差异表通过
     * @param uid
     * @param token
     * @param month
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirmCostDiff")
    @ResponseBody
    public ResultEntity confirmCostDiff(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "month", defaultValue = "") String month) throws Exception{

        this.itScmMonthlyBalanceService.confirmCostDiff(uid,month);
        ResultEntity res = new ResultEntity();
        res.setMsg("成本差异表通过！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 财务和仓储的差异月报
     * @param uid
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/diffCar")
    @ResponseBody
    public Map diffCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestBody Map data )
            throws Exception{
        Map map = this.itScmMonthlyBalanceService.diffCar(start,limit,data,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 月末成本差异表导出
     * @param uid
     * @param date
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportDifferCost")
    public void exportDifferCost( @RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "date", defaultValue = "") String date,
                                   HttpServletRequest request,
                           HttpServletResponse response)
            throws Exception {

        List lists = itScmMonthlyBalanceService.findList(uid,date);

        HSSFWorkbook wb = itScmMonthlyBalanceService.exportCost(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "月末成本差异表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 实物与财务差异表导出
     * @param uid
     * @param token
     * @param date
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportDifferCar")
    public void exportDifferCar( @RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "date", defaultValue = "") String date,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
            throws Exception {

        List lists = itScmMonthlyBalanceService.findLists(uid,date);

        HSSFWorkbook wb = itScmMonthlyBalanceService.exportDiffCar(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "实物与财务差异表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 判断是否未月结并没过指定日期
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/ifMonthEnd")
    public ResultEntity ifMonthEnd(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {

        ResultEntity entity = new ResultEntity();
        boolean flag = this.itScmMonthlyBalanceService.ifMonthBalace(uid);
        Map map = new HashMap();
        map.put("success",flag);
        entity.setSuccess(true);
        entity.setData(map);
        return entity;

    }

}
